﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XChipTool.Enties;

namespace XChip_Common
{
    public static class HeaderHelper
    {

        public delegate void SendEventHeaderHelperMsg(string msg);

        public static event SendEventHeaderHelperMsg SendEvent;

        /// <summary>
        /// 更改heard数据
        /// </summary>
        /// <param name="Patch_FileName_Path">文件名称</param>
        /// <param name="isSetting0_Patch">使能勾选1</param>
        /// <param name="isSetting1_Patch">使能勾选2</param>
        /// <param name="XcBaseList">生成heard 数据</param>
        public static void Process_Patch_File(string Patch_FileName_Path, bool isSetting0_Patch, bool isSetting1_Patch, ref List<xc151> XcBaseList)
        {
            StringBuilder txtBox_bat = new StringBuilder();
            var Setting0Checked = isSetting0_Patch;
            var Setting1Checked = isSetting1_Patch;
            int Flag_Valid = 0;
            int Patch_Lenth = 0;
            bool Flag_Ram_Addr_Get = true;
            try
            {
                StreamReader f2 = new StreamReader(Patch_FileName_Path, System.Text.Encoding.GetEncoding("gb2312"));//gb2312编码，为制定文件名初始化
                while (f2.Peek() > -1)
                {
                    var txt_str = f2.ReadLine().Trim().Replace('，', ','); ;
                    var ssArrary = txt_str.Split(',');
                    if (ssArrary[0].Trim() == "0x0006")
                    {
                        //Patch版本号
                        Flag_Valid = 1;
                        if (Setting0Checked)
                        {
                            var temp = XcBaseList.Where(d => d.xcindexId == 39).ToList()[0].address;
                            XcBaseList.ForEach(c =>
                            { //patch版本号
                                if (c.address == temp)
                                {
                                    c.dataexample = ssArrary[1].Trim().ToLower();
                                    return;
                                }
                            });


                        }
                        if (Setting1Checked)
                        {
                            var temp = XcBaseList.Where(d => d.xcindexId == 56).ToList()[0].address;
                            XcBaseList.ForEach(c =>
                            {
                                //patch版本号
                                if (c.address == temp)
                                {
                                    c.dataexample = ssArrary[1].Trim().ToLower();
                                }
                            });
                        }
                    }
                    else if (ssArrary[0].Trim() == "0x0007")
                    {
                        //Patch版本号
                        if (Setting0Checked)
                        {
                            var temp = XcBaseList.Where(d => d.xcindexId == 39).ToList()[1].address;
                            XcBaseList.ForEach(c =>
                            {
                                if (c.address == temp)
                                {
                                    c.dataexample = ssArrary[1].Trim().ToLower();
                                    return;
                                }
                            });
                        }
                        if (Setting1Checked)
                        {
                            var temp = XcBaseList.Where(d => d.xcindexId == 56).ToList()[1].address;
                            XcBaseList.ForEach(c =>
                            {
                                if (c.address == temp)
                                {
                                    c.dataexample = ssArrary[1].Trim().ToLower();
                                }
                            });
                        }
                    }
                    //else if (ssArrary[0].Trim() == "0x000c")
                    //{
                    //    if (Setting0Checked)
                    //    {
                    //         XcBaseList .ForEach(c =>
                    //        {
                    //            if (c.address == "0x004A")
                    //            {
                    //                c.dataexample = ssArrary[1].Trim();
                    //            }
                    //        });
                    //    }
                    //    if (Setting1Checked)
                    //    {
                    //         XcBaseList .ForEach(c =>
                    //        {
                    //            if (c.address == "0x008A")
                    //            {
                    //                c.dataexample = ssArrary[1].Trim();
                    //            }
                    //        });
                    //    }
                    //}
                    else if (ssArrary[0].Trim() == "0x000d")
                    {
                        //Patch的入口地址（32bit）
                        if (Setting0Checked)
                        {
                            var temp = XcBaseList.Where(d => d.xcindexId == 40).ToList()[1].address;
                            XcBaseList.ForEach(c =>
                            {
                                //if (c.address == "0x0051")
                                if (c.address == temp)
                                {
                                    c.dataexample = ssArrary[1].Trim().ToLower();
                                }
                            });
                        }
                        if (Setting1Checked)
                        {
                            var temp = XcBaseList.Where(d => d.xcindexId == 57).ToList()[1].address;
                            XcBaseList.ForEach(c =>
                            {
                                if (c.address == temp)
                                {
                                    c.dataexample = ssArrary[1].Trim().ToLower();
                                }
                            });
                        }
                    }
                    else if (ssArrary[0].Trim() == "0x000e")
                    {
                        //Patch的入口地址（32bit）
                        if (Setting0Checked)
                        {
                            var temp = XcBaseList.Where(d => d.xcindexId == 40).ToList()[2].address;
                            XcBaseList.ForEach(c =>
                            {
                                if (c.address == temp)
                                {
                                    c.dataexample = ssArrary[1].Trim().ToLower();
                                }
                            });
                        }
                        if (Setting1Checked)
                        {
                            var temp = XcBaseList.Where(d => d.xcindexId == 57).ToList()[2].address;
                            XcBaseList.ForEach(c =>
                            {
                                if (c.address == temp)
                                {
                                    c.dataexample = ssArrary[1].Trim().ToLower();
                                }
                            });
                        }
                    }
                    else if (ssArrary[0].Trim() == "0x000f")
                    {
                        //Patch的入口地址（32bit）
                        if (Flag_Valid == 1) Flag_Valid = 2;
                        if (Setting0Checked)
                        {
                            var temp = XcBaseList.Where(d => d.xcindexId == 40).ToList()[3].address;
                            XcBaseList.ForEach(c =>
                            {
                                if (c.address == temp)
                                {
                                    c.dataexample = ssArrary[1].Trim().ToLower();
                                }
                            });
                        }
                        if (Setting1Checked)
                        {
                            var temp = XcBaseList.Where(d => d.xcindexId == 57).ToList()[3].address;
                            XcBaseList.ForEach(c =>
                            {
                                if (c.address == temp)
                                {
                                    c.dataexample = ssArrary[1].Trim().ToLower();
                                }
                            });
                        }
                    }
                    else
                    {
                        if (Flag_Valid == 2)
                        {
                            Patch_Lenth++;
                            if (Flag_Ram_Addr_Get)
                            {
                                //写入RAM中的目的地址
                                Flag_Ram_Addr_Get = false;
                                if (Setting0Checked)
                                {
                                    var temp = XcBaseList.Where(d => d.xcindexId == 37).ToList();
                                    XcBaseList.ForEach(c =>
                                    {
                                        if (c.address == temp[2].address)
                                        {
                                            var str = ssArrary[1];
                                            //byte byt = (byte)((Convert.ToUInt16(ssArrary[0].Trim(), 16) >> 8) & 0xff);
                                            //var str1 = System.Text.Encoding.Default.GetString(byt);
                                            c.dataexample = "0x" + ((Convert.ToUInt16(ssArrary[0].Trim(), 16) >> 8 & 0xff)).ToString("X2").ToLower();
                                        }
                                        if (c.address == temp[3].address)
                                        {
                                            c.dataexample = "0x" + ((Convert.ToUInt16(ssArrary[0].Trim(), 16) >> 0 & 0xff)).ToString("X2").ToLower();
                                        }
                                    });
                                }
                                if (Setting1Checked)
                                {
                                    var temp = XcBaseList.Where(d => d.xcindexId == 54).ToList();
                                    XcBaseList.ForEach(c =>
                                    {
                                        if (c.address == temp[2].address)
                                        {
                                            c.dataexample = "0x" + ((Convert.ToUInt16(ssArrary[0].Trim(), 16) >> 8 & 0xff)).ToString("X2").ToLower();
                                        }
                                        if (c.address == temp[3].address)
                                        {
                                            c.dataexample = "0x" + ((Convert.ToUInt16(ssArrary[0].Trim(), 16) >> 0 & 0xff)).ToString("X2").ToLower();
                                        }
                                    });
                                }
                            }

                        }
                    }
                }
                //补齐能否被4整除
                if ((Patch_Lenth % 4) != 0)
                {
                    UInt16 temp;
                    temp = (UInt16)(Patch_Lenth + 4);
                    temp = (UInt16)(temp / 4);
                    temp = (UInt16)(temp * 4);
                    Patch_Lenth = temp;
                }
                //给补丁长度赋值
                if (Setting0Checked)
                {
                    var temp = XcBaseList.Where(d => d.xcindexId == 38).ToList();
                    //Patch长度
                    XcBaseList.ForEach(c =>
                    {
                        if (c.address == temp[0].address)
                        {
                            c.dataexample = "0x" + (Patch_Lenth >> 8 & 0xff).ToString("X2").ToLower();
                        }
                        if (c.address == temp[1].address)
                        {
                            c.dataexample = "0x" + (Patch_Lenth >> 0 & 0xff).ToString("X2").ToLower();
                        }
                    });
                    //SettingList_Help.SettingBaseList.Setting0_Patch.lengthAddress = Convert.ToUInt32(Patch_Lenth).ToString("X4");
                }
                if (Setting1Checked)
                {
                    var temp = XcBaseList.Where(d => d.xcindexId == 55).ToList();
                    XcBaseList.ForEach(c =>
                    {
                        if (c.address == temp[0].address)
                        {
                            c.dataexample = "0x" + (Patch_Lenth >> 8 & 0xff).ToString("X2").ToLower();
                        }
                        if (c.address == temp[1].address)
                        {
                            c.dataexample = "0x" + (Patch_Lenth >> 0 & 0xff).ToString("X2").ToLower();
                        }
                    });
                    //SettingList_Help.SettingBaseList.Setting1_Patch.lengthAddress = Convert.ToUInt32(Patch_Lenth).ToString("X4");
                }
            }
            catch (Exception ex)
            {
                if (null != SendEvent)
                {
                    SendEvent(ex.Message.ToString());
                }

            }
        }


        /// <summary>
        /// 更具路径输出截取后的heard内容
        /// </summary>
        /// <param name="Patch_FileName_Path"></param>
        /// <returns></returns>
        public static string CreatPatchText(string Patch_FileName_Path)
        {
            StringBuilder txtBox_bat = new StringBuilder();
            int Flag_Valid = 0;
            try
            {
                StreamReader f2 = new StreamReader(Patch_FileName_Path, System.Text.Encoding.GetEncoding("gb2312"));//gb2312编码，为制定文件名初始化
                while (f2.Peek() > -1)
                {
                    var txt_str = f2.ReadLine().Trim().Replace('，', ',');   ;
                    var ssArrary = txt_str.Split(',');
                    if (ssArrary.Length > 2)
                    {
                        if (ssArrary[0].Trim() == "0x0006")
                        {
                            Flag_Valid = 1;
                        }
                        else if (ssArrary[0].Trim() == "0x000f")
                        {
                            if (Flag_Valid == 1)
                            {
                                Flag_Valid = 2;
                            }
                        }
                        else
                        {
                            if (Flag_Valid == 2)
                            {
                                txtBox_bat.Append(txt_str);
                                txtBox_bat.Append("\r\n");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (null != SendEvent)
                {
                    SendEvent(ex.Message.ToString());
                }
            }

            return txtBox_bat.ToString();
        }

        /// <summary>
        /// 更具文件路径和内容生成文件
        /// </summary>
        /// <param name="Patch_FileName_Path"></param>
        /// <param name="contentTxt"></param>
        public static void SavePatchText(string Patch_FileName_Path, string contentTxt)
        {
            using (StreamWriter sw = File.CreateText(Patch_FileName_Path))
            {
                sw.WriteLine(contentTxt);
                sw.Flush();
                sw.Close();
            }
        }

        /// <summary>
        /// 更具文件路劲 获取文件长度
        /// </summary>
        /// <param name="mpath">16进制字符串</param>
        /// <returns></returns>
        public static string FilePathLengthComplement(string mpath)
        {
            
            int tempLength = File.ReadAllLines(mpath).Length;
            var tempCount = Convert.ToUInt32((tempLength * 4).ToString("x4"), 16);
            //补齐能否被4整除
            UInt32 temp = tempCount / 0x1000;
            UInt32 tempy = tempCount % 0x1000;
            if (tempy > 0)
            {
                ++temp;
            }
            return (temp * 4096).ToString("X4");
        }
        /// <summary>
        /// 更具文件路劲 获取文件长度
        /// </summary>
        /// <param name="mpath">16进制字符串</param>
        /// <returns></returns>
        public static string FilePathLengthComplementnew(string mpath)
        {

            int tempLength = File.ReadAllLines(mpath).Length;
            var tempCount = Convert.ToUInt32((tempLength * 4).ToString("x4"), 16);
            return tempCount.ToString("X4");
        }

        /// <summary>
        /// 随机长度计算公式
        /// </summary>
        /// <param name="mpath"></param>
        /// <returns></returns>
        public static string FilePathLengthRandomComplement(string mpath)
        {
            int tempLength = File.ReadAllLines(mpath).Length;
            var tempCount = Convert.ToUInt32((tempLength * 4).ToString("x4"), 16);
            //补齐能否被4整除
            UInt32 temp = tempCount / 0x1000;
            UInt32 tempy = tempCount % 0x1000;
            if (tempy > 0)
            {
                //var tempdit =  0x1000 - (tempCount - (temp * 0x1000));
                var tempNum = Convert.ToInt32(tempy.ToString());
                Random rd = new Random();
                int i = rd.Next(tempNum, 4096);

                var temprd = (temp * 4096 + Convert.ToUInt32(i.ToString("x4"), 16));
                while (temprd % 0x4 != 0 && temprd % 0x8 != 0 && temprd % 0xc != 0)
                {
                    i = rd.Next(tempNum, 4096);
                    temprd = (temp * 4096 + Convert.ToUInt32(i.ToString("x4"), 16));
                }
                return (temp * 4096 + Convert.ToUInt32(i.ToString("x4"), 16)).ToString("X4");
            }
            else
            {
                return (temp * 4096).ToString("X4");
            }

        }
        /// <summary>
        /// 头文件计算长度   行车使用
        /// </summary>
        /// <returns></returns>
        public static string FilePathRandomHeard()
        {
            var tempCount = Convert.ToUInt32("104", 16);
            //补齐能否被4整除
            UInt32 temp = tempCount / 0x1000;
            UInt32 tempy = tempCount % 0x1000;
            if (tempy > 0)
            {
                //var tempdit = 0x1000 - (tempCount - (temp * 0x1000));
                var tempNum = Convert.ToInt32(tempCount.ToString());
                Random rd = new Random();
                int i = rd.Next(tempNum, 4096);
                var temprd = (temp * 4096 + Convert.ToUInt32(i.ToString("x4"), 16));
                while (temprd % 0x4 != 0 && temprd % 0x8 != 0 && temprd % 0xc != 0)
                {
                    i = rd.Next(tempNum, 4096);
                    temprd = (temp * 4096 + Convert.ToUInt32(i.ToString("x4"), 16));
                }
                return (temp * 4096 + Convert.ToUInt32(i.ToString("x4"), 16)).ToString("X4");
            }
            else
            {
                return (temp * 4096).ToString("X4");
            }
        }
        /// <summary>
        /// 文件计算长度
        /// </summary>
        /// <param name="mpath"></param>
        /// <returns></returns>
        public static string FilePathLength(string mpath)
        {
            int tempLength = File.ReadAllLines(mpath).Length;
            return (tempLength * 4).ToString("x4");
        }


        /// <summary>
        /// 根据list集合生成对应文件脚本
        /// </summary>
        /// <param name="XcBaseList"></param>
        /// <returns></returns>
        public static string GenerateHeaderFile(List<xc151> XcBaseList)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in XcBaseList)
            {
                sb.Append(item.address + "," + item.dataexample + ",");
                sb.Append("\r\n");
            }
            return sb.ToString();
        }
        public static string GenerateHeader128_1File(List<xc151> XcBaseList)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in XcBaseList)
            {
                sb.Append(item.address + "," + item.dataexample + ",");
                sb.Append("\r\n");
            }
            return sb.ToString();
        }


        public static string GenerateHeader128_1_64File(List<xc151> XcBaseList)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in XcBaseList)
            {
                sb.Append(item.address + "," + item.dataexample + ",");
                sb.Append("\r\n");
            }
            return sb.ToString();
        }
        /// <summary>
        /// 设置是否开启使能开关
        /// </summary>
        /// <param name="address"></param>
        /// <param name="Checked"></param>
        /// <param name="XcBaseList"></param>
        public static void setDataexample(string address, bool Checked, ref List<xc151> XcBaseList)
        {
            if (null != XcBaseList)
            {
                XcBaseList.ForEach(c =>
                {
                    if (c.address == address)
                    {
                        c.dataexample = Checked ? "0x01" : "0x00";
                        return;
                    }
                });
            }
        }

        public static void SetXcBase(string _address, string _dataexample, ref List<xc151> XcBaseList)
        {
            XcBaseList.ForEach(c =>
            {
                if (c.address == _address)
                {
                    c.dataexample = string.Format("0x{0}", _dataexample);
                    return;
                }
            });
        }

        public static void SetAddressLength(string showName, string serialno, string headaddress, string longitude, ref List<xc151> XcBaseList)
        {

            List<string> listNumber = SettingList_Help.HexStringToStringList(serialno);
            List<string> listFirst = SettingList_Help.HexStringToStringList(headaddress);
            List<string> listlength = SettingList_Help.HexStringToStringList(longitude);
            switch (showName)
            {
                case "Setting0_Init"://setting0区配置0
                    {
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 24).ToList()[0].address, listNumber[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 24).ToList()[1].address, listNumber[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 24).ToList()[2].address, listNumber[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 24).ToList()[3].address, listNumber[3], ref XcBaseList);

                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 25).ToList()[0].address, listFirst[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 25).ToList()[1].address, listFirst[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 25).ToList()[2].address, listFirst[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 25).ToList()[3].address, listFirst[3], ref XcBaseList);

                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 26).ToList()[0].address, listlength[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 26).ToList()[1].address, listlength[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 26).ToList()[2].address, listlength[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 26).ToList()[3].address, listlength[3], ref XcBaseList);

                    }
                    break;
                case "Setting0_Sensor"://setting0区配置1
                    {
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 28).ToList()[0].address, listNumber[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 28).ToList()[1].address, listNumber[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 28).ToList()[2].address, listNumber[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 28).ToList()[3].address, listNumber[3], ref XcBaseList);

                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 29).ToList()[0].address, listFirst[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 29).ToList()[1].address, listFirst[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 29).ToList()[2].address, listFirst[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 29).ToList()[3].address, listFirst[3], ref XcBaseList);

                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 30).ToList()[0].address, listlength[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 30).ToList()[1].address, listlength[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 30).ToList()[2].address, listlength[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 30).ToList()[3].address, listlength[3], ref XcBaseList);

                    }
                    break;
                case "Setting0_IQ"://setting0区配置2
                    {
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 32).ToList()[0].address, listNumber[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 32).ToList()[1].address, listNumber[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 32).ToList()[2].address, listNumber[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 32).ToList()[3].address, listNumber[3], ref XcBaseList);

                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 33).ToList()[0].address, listFirst[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 33).ToList()[1].address, listFirst[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 33).ToList()[2].address, listFirst[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 33).ToList()[3].address, listFirst[3], ref XcBaseList);

                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 34).ToList()[0].address, listlength[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 34).ToList()[1].address, listlength[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 34).ToList()[2].address, listlength[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 34).ToList()[3].address, listlength[3], ref XcBaseList);

                    }
                    break;
                case "Setting1_Init"://setting1区配置0
                    {
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 41).ToList()[0].address, listNumber[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 41).ToList()[1].address, listNumber[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 41).ToList()[2].address, listNumber[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 41).ToList()[3].address, listNumber[3], ref XcBaseList);

                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 42).ToList()[0].address, listFirst[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 42).ToList()[1].address, listFirst[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 42).ToList()[2].address, listFirst[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 42).ToList()[3].address, listFirst[3], ref XcBaseList);

                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 43).ToList()[0].address, listlength[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 43).ToList()[1].address, listlength[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 43).ToList()[2].address, listlength[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 43).ToList()[3].address, listlength[3], ref XcBaseList);

                    }
                    break;
                case "Setting1_Sensor"://setting1区配置1
                    {
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 45).ToList()[0].address, listNumber[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 45).ToList()[1].address, listNumber[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 45).ToList()[2].address, listNumber[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 45).ToList()[3].address, listNumber[3], ref XcBaseList);

                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 46).ToList()[0].address, listFirst[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 46).ToList()[1].address, listFirst[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 46).ToList()[2].address, listFirst[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 46).ToList()[3].address, listFirst[3], ref XcBaseList);

                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 47).ToList()[0].address, listlength[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 47).ToList()[1].address, listlength[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 47).ToList()[2].address, listlength[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 47).ToList()[3].address, listlength[3], ref XcBaseList);
                    }
                    break;
                case "Setting1_IQ"://setting1区配置2
                    {
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 49).ToList()[0].address, listNumber[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 49).ToList()[1].address, listNumber[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 49).ToList()[2].address, listNumber[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 49).ToList()[3].address, listNumber[3], ref XcBaseList);

                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 50).ToList()[0].address, listFirst[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 50).ToList()[1].address, listFirst[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 50).ToList()[2].address, listFirst[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 50).ToList()[3].address, listFirst[3], ref XcBaseList);

                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 51).ToList()[0].address, listlength[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 51).ToList()[1].address, listlength[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 51).ToList()[2].address, listlength[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 51).ToList()[3].address, listlength[3], ref XcBaseList);
                    }
                    break;
                case "Setting0_Patch":
                    {
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 36).ToList()[0].address, listNumber[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 36).ToList()[1].address, listNumber[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 36).ToList()[2].address, listNumber[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 36).ToList()[3].address, listNumber[3], ref XcBaseList);
                    }
                    break;
                case "Setting1_Patch":
                    {
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 53).ToList()[0].address, listNumber[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 53).ToList()[1].address, listNumber[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 53).ToList()[2].address, listNumber[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 53).ToList()[3].address, listNumber[3], ref XcBaseList);
                    }
                    break;
                case "OSD"://OSD数据区
                    {


                        //osd保存退出1
                        var listtwo = SettingList_Help.HexStringToStringList((Convert.ToInt32(serialno, 16) - 0x4000).ToString("x8"));
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 58).ToList()[0].address, listtwo[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 58).ToList()[1].address, listtwo[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 58).ToList()[2].address, listtwo[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 58).ToList()[3].address, listtwo[3], ref XcBaseList);

                        //osd保存退出2
                        var listthree = SettingList_Help.HexStringToStringList((Convert.ToInt32(serialno, 16) - 0x3000).ToString("x8"));
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 59).ToList()[0].address, listthree[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 59).ToList()[1].address, listthree[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 59).ToList()[2].address, listthree[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 59).ToList()[3].address, listthree[3], ref XcBaseList);

                        //osd保存退出3
                        var listfour = SettingList_Help.HexStringToStringList((Convert.ToInt32(serialno, 16) - 0x2000).ToString("x8"));
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 60).ToList()[0].address, listfour[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 60).ToList()[1].address, listfour[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 60).ToList()[2].address, listfour[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 60).ToList()[3].address, listfour[3], ref XcBaseList);
                        //osd出场数据
                        var listfive = SettingList_Help.HexStringToStringList((Convert.ToInt32(serialno, 16) - 0x1000).ToString("x8"));
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 61).ToList()[0].address, listfive[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 61).ToList()[1].address, listfive[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 61).ToList()[2].address, listfive[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 61).ToList()[3].address, listfive[3], ref XcBaseList);
                        //语言编号
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 62).ToList()[0].address, listNumber[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 62).ToList()[1].address, listNumber[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 62).ToList()[2].address, listNumber[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 62).ToList()[3].address, listNumber[3], ref XcBaseList);
                        //菜单首地址
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 63).ToList()[0].address, listFirst[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 63).ToList()[1].address, listFirst[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 63).ToList()[2].address, listFirst[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 63).ToList()[3].address, listFirst[3], ref XcBaseList);

                        //菜单长度
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 64).ToList()[0].address, "00", ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 64).ToList()[1].address, "00", ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 64).ToList()[2].address, "30", ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 64).ToList()[3].address, "00", ref XcBaseList);

                        //字库首地址
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 65).ToList()[0].address, listlength[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 65).ToList()[1].address, listlength[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 65).ToList()[2].address, listlength[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 65).ToList()[3].address, listlength[3], ref XcBaseList);

                        //字库长度
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 66).ToList()[0].address, "00", ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 66).ToList()[1].address, "00", ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 66).ToList()[2].address, "90", ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 66).ToList()[3].address, "00", ref XcBaseList);

                    }
                    break;
                case "Spare0"://备用数据区0
                    {
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 68).ToList()[0].address, listNumber[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 68).ToList()[1].address, listNumber[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 68).ToList()[2].address, listNumber[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 68).ToList()[3].address, listNumber[3], ref XcBaseList);

                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 69).ToList()[0].address, listFirst[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 69).ToList()[1].address, "14", ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 69).ToList()[2].address, listFirst[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 69).ToList()[3].address, listFirst[3], ref XcBaseList);

                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 70).ToList()[0].address, listlength[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 70).ToList()[1].address, listlength[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 70).ToList()[2].address, listlength[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 70).ToList()[3].address, listlength[3], ref XcBaseList);
                    }
                    break;
                case "Spare1"://备用数据区1
                    {
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 72).ToList()[0].address, listNumber[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 72).ToList()[1].address, listNumber[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 72).ToList()[2].address, listNumber[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 72).ToList()[3].address, listNumber[3], ref XcBaseList);

                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 73).ToList()[0].address, listFirst[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 73).ToList()[1].address, "14", ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 73).ToList()[2].address, listFirst[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 73).ToList()[3].address, listFirst[3], ref XcBaseList);

                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 74).ToList()[0].address, listlength[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 74).ToList()[1].address, listlength[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 74).ToList()[2].address, listlength[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 74).ToList()[3].address, listlength[3], ref XcBaseList);
                    }
                    break;
                case "Spare2"://备用数据区2
                    {
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 76).ToList()[0].address, listNumber[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 76).ToList()[1].address, listNumber[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 76).ToList()[2].address, listNumber[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 76).ToList()[3].address, listNumber[3], ref XcBaseList);

                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 77).ToList()[0].address, listFirst[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 77).ToList()[1].address, "14", ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 77).ToList()[2].address, listFirst[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 77).ToList()[3].address, listFirst[3], ref XcBaseList);

                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 78).ToList()[0].address, listlength[0], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 78).ToList()[1].address, listlength[1], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 78).ToList()[2].address, listlength[2], ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 78).ToList()[3].address, listlength[3], ref XcBaseList);
                    }
                    break;
                case "Key_Path":
                    {
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 10).ToList()[0].address, listNumber[0].ToUpper(), ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 10).ToList()[1].address, listNumber[1].ToUpper(), ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 10).ToList()[2].address, listNumber[2].ToUpper(), ref XcBaseList);
                        SetXcBase(XcBaseList.Where(c => c.xcindexId == 10).ToList()[3].address, listNumber[3].ToUpper(), ref XcBaseList);
                    }
                    break;
                default:
                    break;
            }
        }



        public static void SetHeaderRandomMask(ref List<xc151> XcBaseList)
        {
            Random rd = new Random();
            //setting区
            #region setting0区0
            if (XcBaseList.Where(c => c.xcindexId == 12).ToList()[0].dataexample == "0x00") //setting0区0：
            {
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 24).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 24).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 24).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 24).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                SetXcBase(XcBaseList.Where(c => c.xcindexId == 25).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 25).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 25).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 25).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                SetXcBase(XcBaseList.Where(c => c.xcindexId == 26).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 26).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 26).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 26).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);
            }
            #endregion
            #region setting0区1
            if (XcBaseList.Where(c => c.xcindexId == 13).ToList()[0].dataexample == "0x00") //setting0区1：
            {
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 28).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 28).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 28).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 28).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                SetXcBase(XcBaseList.Where(c => c.xcindexId == 29).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 29).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 29).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 29).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                SetXcBase(XcBaseList.Where(c => c.xcindexId == 30).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 30).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 30).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 30).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);
            }
            #endregion
            #region setting0区2
            if (XcBaseList.Where(c => c.xcindexId == 14).ToList()[0].dataexample == "0x00")//setting0区2：
            {
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 32).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 32).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 32).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 32).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                SetXcBase(XcBaseList.Where(c => c.xcindexId == 33).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 33).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 33).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 33).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                SetXcBase(XcBaseList.Where(c => c.xcindexId == 34).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 34).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 34).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 34).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);
            }
            #endregion
            #region setting1区0
            if (XcBaseList.Where(c => c.xcindexId == 16).ToList()[0].dataexample == "0x00")//setting1区0：
            {
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 41).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 41).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 41).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 41).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                SetXcBase(XcBaseList.Where(c => c.xcindexId == 42).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 42).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 42).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 42).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                SetXcBase(XcBaseList.Where(c => c.xcindexId == 43).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 43).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 43).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 43).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);
            }
            #endregion
            #region setting1区1
            if (XcBaseList.Where(c => c.xcindexId == 17).ToList()[0].dataexample == "0x00")//setting1区1：
            {
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 45).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 45).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 45).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 45).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                SetXcBase(XcBaseList.Where(c => c.xcindexId == 46).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 46).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 46).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 46).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                SetXcBase(XcBaseList.Where(c => c.xcindexId == 47).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 47).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 47).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 47).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);
            }
            #endregion
            #region setting1区2
            if (XcBaseList.Where(c => c.xcindexId == 18).ToList()[0].dataexample == "0x00") //setting1区2：
            {
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 49).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 49).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 49).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 49).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                SetXcBase(XcBaseList.Where(c => c.xcindexId == 50).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 50).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 50).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 50).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                SetXcBase(XcBaseList.Where(c => c.xcindexId == 51).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 51).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 51).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 51).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);
            }
            #endregion
            #region setting0区_补丁
            if (XcBaseList.Where(c => c.xcindexId == 15).ToList()[0].dataexample == "0x00")//setting0区_补丁
            {
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 36).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 36).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 36).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 36).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);
            }
            #endregion
            #region setting1区_补丁
            if (XcBaseList.Where(c => c.xcindexId == 19).ToList()[0].dataexample == "0x00")//setting1区_补丁：
            {
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 53).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 53).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 53).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 53).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);
            }
            #endregion
            #region OSD
            if (XcBaseList.Where(c => c.xcindexId == 20).ToList()[0].dataexample == "0x00")
            {
                //osd保存退出1
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 58).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 58).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 58).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 58).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                //osd保存退出2
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 59).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 59).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 59).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 59).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                //osd保存退出3
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 60).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 60).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 60).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 60).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);
                //osd出场数据
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 61).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 61).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 61).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 61).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);
                //语言编号
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 62).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 62).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 62).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 62).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);
                //菜单首地址
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 63).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 63).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 63).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 63).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                //菜单长度
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 64).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 64).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 64).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 64).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                //字库首地址
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 65).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 65).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 65).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 65).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                //字库长度
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 66).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 66).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 66).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 66).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);
            }
            #endregion
            #region spare1
            if (XcBaseList.Where(c => c.xcindexId == 21).ToList()[0].dataexample == "0x00")
            {
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 68).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 68).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 68).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 68).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                SetXcBase(XcBaseList.Where(c => c.xcindexId == 69).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 69).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 69).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 69).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                SetXcBase(XcBaseList.Where(c => c.xcindexId == 70).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 70).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 70).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 70).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);
            }
            #endregion
            #region spare2
            if (XcBaseList.Where(c => c.xcindexId == 22).ToList()[0].dataexample == "0x00")
            {
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 72).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 72).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 72).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 72).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                SetXcBase(XcBaseList.Where(c => c.xcindexId == 73).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 73).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 73).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 73).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                SetXcBase(XcBaseList.Where(c => c.xcindexId == 74).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 74).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 74).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 74).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);
            }
            #endregion
            #region spare3
            if (XcBaseList.Where(c => c.xcindexId == 23).ToList()[0].dataexample == "0x00")
            {
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 76).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 76).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 76).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 76).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                SetXcBase(XcBaseList.Where(c => c.xcindexId == 77).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 77).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 77).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 77).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);

                SetXcBase(XcBaseList.Where(c => c.xcindexId == 78).ToList()[0].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 78).ToList()[1].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 78).ToList()[2].address, rd.Next().ToString("x2"), ref XcBaseList);
                SetXcBase(XcBaseList.Where(c => c.xcindexId == 78).ToList()[3].address, rd.Next().ToString("x2"), ref XcBaseList);
            }
            #endregion

        }
        /// <summary>
        /// 把bin文件转换成txt
        /// </summary>
        /// <param name="file_pat"></param>
        public static void BinConversionTXT(string file_path)
        {

            int read_len;
            byte[] binchar = new byte[] { };
            FileStream Myfile = new FileStream(file_path, FileMode.Open, FileAccess.Read);
            BinaryReader binreader = new BinaryReader(Myfile);
            int file_len = (int)Myfile.Length;//获取bin文件长度
            List<string> list = new List<string>();
            while (file_len > 0)
            {
                if (file_len / 256 > 0)//一次读取256字节
                    read_len = 256;
                else                   //不足256字节按实际长度读取
                    read_len = file_len % 256;
                binchar = binreader.ReadBytes(read_len);
                foreach (byte j in binchar)
                {
                    list.Add(j.ToString("X2")); //大写 8位显示 增加前导0

                }
                file_len -= read_len;
            }

            binreader.Close();
            Myfile.Close();
            string pSavePath = System.IO.Path.GetDirectoryName(file_path); //获取文件路径
            string pXchip_Name = System.IO.Path.GetFileName(file_path); //获取文件名
            string path = string.Format("{0}\\{1}.txt", pSavePath, pXchip_Name);
            System.IO.File.WriteAllText(path, string.Join("\n", list.ToArray()), Encoding.Default);
        }
    }
}
